package com.s2.m.btchat;

import java.io.IOException;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextField;

/**
 *
 * @author Samik
 */
public class MyProfile implements CommandListener {

    private Display display;
    private static String username = "User1";
    private static String fullName = "";
    private static String aboutMe = "";
    private static String age = "0";
    private static String sex = "Unspecified";
    private static String profession = "N/A";
    private static boolean modified = false;
    private Form form = new Form("Profile");
    private TextField usernameField;
    private TextField fullNameField;
    private TextField aboutMeField;
    private TextField ageField;
    private ChoiceGroup sexChoiceGroup;
    private Image[] sexImgs = new Image[2];
    private String[] sexStrings = {"Male", "Female"};
    private TextField professionField;
    private Command updateCommand = new Command("Update", Command.SCREEN, 0);
    private Command cancelCommand = new Command("Cancel", Command.CANCEL, 1);
    /**
     * Keep the parent reference
     */
    BTChat parent;

    public MyProfile(BTChat parent) {
        this.parent = parent;
        display = Display.getDisplay(parent);

        usernameField = new TextField("Username", username,
                10, TextField.ANY);
        fullNameField = new TextField("Full Name", fullName,
                50, TextField.ANY);
        aboutMeField = new TextField("About Me", aboutMe,
                200, TextField.ANY);
        ageField = new TextField("Age", age, 3, TextField.NUMERIC);

        professionField = new TextField("Profession", profession, 50,
                TextField.ANY);

        int h = display.getBestImageHeight(Display.CHOICE_GROUP_ELEMENT);
        String dim = (h >= 16) ? "16x16" : "12x12";

        try {
            sexImgs[0] = Image.createImage("/com/s2/m/res/img/male" + dim + ".png");
            sexImgs[1] = Image.createImage("/com/s2/m/res/img/female" + dim + ".png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        sexChoiceGroup = new ChoiceGroup("Sex", ChoiceGroup.EXCLUSIVE,
                sexStrings, sexImgs);
        if (sex.equals("Female")) {
            sexChoiceGroup.setSelectedIndex(1, true);
        } else {
            sexChoiceGroup.setSelectedIndex(0, true);
        }
        form.append(usernameField);
        form.append(fullNameField);
        form.append(aboutMeField);
        form.append(ageField);
        form.append(sexChoiceGroup);
        form.append(professionField);

        form.addCommand(updateCommand);
        form.addCommand(cancelCommand);

        form.setCommandListener(this);
    }

    public static String getAboutMe() {
        return aboutMe;
    }

    public static String getAge() {
        return age;
    }

    public static String getFullName() {
        return fullName;
    }

    public static String getProfession() {
        return profession;
    }

    public static String getSex() {
        return sex;
    }

    public static String getUsername() {
        return username;
    }

    public static void setAboutMe(String aboutMe) {
        MyProfile.aboutMe = aboutMe;
    }

    public static void setAge(String age) {
        MyProfile.age = age;
    }

    public static void setFullName(String fullName) {
        MyProfile.fullName = fullName;
    }

    public static void setProfession(String profession) {
        MyProfile.profession = profession;
    }

    public static void setSex(String sex) {
        MyProfile.sex = sex;
    }

    public static void setUsername(String username) {
        MyProfile.username = username;
    }

    public void commandAction(Command c, Displayable d) {
        if (c == updateCommand) {
            setUsername(usernameField.getString());
            setFullName(fullNameField.getString());
            setAboutMe(aboutMeField.getString());
            setAge(ageField.getString());
            setSex(sexChoiceGroup.getString(sexChoiceGroup.getSelectedIndex()));
            setProfession(professionField.getString());

            if (Server.isRunning()) {
                parent.getChatServer().updateServiceRecord();
            }

            modified = true;
            parent.showUI();
        } else if (c == cancelCommand) {
            parent.showUI();
        }
    }

    public static void setModified(boolean modified) {
        MyProfile.modified = modified;
    }

    public void show() {
        Display.getDisplay(parent).setCurrent(form);
    }

    public static boolean isModified() {
        return modified;
    }
}
